home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-07 | 6.4 KB | 213 lines |
- /*
- PC Plus sample Java application.
- Demonstrates how to transfer items between two Vectors by
- boxes by double-clicking List boxes. Implements a basic
- 'shopping list' manager.
- */
-
- import java.awt.*;
- import java.util.*; // Class Vector is in java.util
-
- public class Frame1 extends Frame {
-
- // Declare two Vectors to main a stock list and a list of items purchased
- Vector thingsInStock;
- Vector thingsInBasket;
- // Note, these Vectors are not actually available for use until they are
- // created with 'new'. In this project, this is done in the Frame's 'show'
- // method. However, the Vectors could have been created at the time of declaration
- // using the following statements instead of the declarations above:
- // Vector thingsInStock = new Vector();
- // Vector thingsInBasket = new Vector();
-
-
- void MoveObFromV1ToV2(Object Ob, Vector V1, Vector V2 ) {
- // Move an item from Vector V1 to Vector V2
- V2.addElement(Ob);
- V1.removeElement(Ob);
- }
-
-
- void BasketList_DblClick(Event event) {
- // transfer selected item from thingsInBasket vector to thingsInStock vector
- // and display the change in the list boxes
- Object thing = thingsInBasket.elementAt(BasketList.getSelectedIndex());
- MoveObFromV1ToV2(thing, thingsInBasket, thingsInStock );
- UpdateListBoxes();
-
- }
-
- void StockList_DblClick(Event event) {
- // transfer selected item from thingsInStock vector to thingsInBasket vector
- // and display the change in the list boxes
- Object thing = thingsInStock.elementAt(StockList.getSelectedIndex());
- MoveObFromV1ToV2(thing, thingsInStock, thingsInBasket );
- UpdateListBoxes();
- }
-
- void UpdateListBox( List l, Vector v) {
- // Enumerate through the elements in Vector v and add them,
- // as strings, to the List l
- l.clear();
- for (Enumeration e = v.elements(); e.hasMoreElements(); ) {
- l.addItem(e.nextElement().toString());
- }
- }
-
- void UpdateListBoxes() {
- // Call UpdateListBox with the Lists and the Vectors representing
- // the things in the Basket and the things in Stock
- UpdateListBox(BasketList, thingsInBasket);
- UpdateListBox(StockList, thingsInStock);
- }
-
- void Open_Action(Event event) {
- OpenFileDialog.show();
- }
-
- void About_Action(Event event) {
- (new AboutDialog(this, "About...", false)).show();
- }
-
- void Exit_Action(Event event) {
- (new QuitDialog(this, "Quit the Application?", false)).show();
- }
-
- public Frame1() {
-
- //{{INIT_CONTROLS
- setLayout(null);
- addNotify();
- resize(insets().left + insets().right + 457,insets().top + insets().bottom + 322);
- OpenFileDialog = new java.awt.FileDialog(this, "Open",FileDialog.LOAD);
- StockList = new java.awt.List(0,false);
- add(StockList);
- StockList.reshape(insets().left + 14,insets().top + 45,201,225);
- BasketList = new java.awt.List(0,false);
- add(BasketList);
- BasketList.reshape(insets().left + 238,insets().top + 45,201,225);
- label1 = new java.awt.Label("ITEMS IN STOCK");
- label1.reshape(insets().left + 14,insets().top + 15,196,15);
- add(label1);
- label2 = new java.awt.Label("ITEMS IN YOUR BASKET");
- label2.reshape(insets().left + 238,insets().top + 15,203,15);
- add(label2);
- label3 = new java.awt.Label("DOUBLE-CLICK TO BUY");
- label3.reshape(insets().left + 14,insets().top + 278,203,22);
- add(label3);
- label4 = new java.awt.Label("DOUBLE-CLICK TO PUT BACK");
- label4.reshape(insets().left + 238,insets().top + 278,203,24);
- add(label4);
- setTitle("A Basic Application");
- //}}
-
- //{{INIT_MENUS
- mainMenuBar = new java.awt.MenuBar();
-
- menu1 = new java.awt.Menu("File");
- menu1.add("Open...");
- menu1.add("Save");
- menu1.add("Save As...");
- menu1.addSeparator();
- menu1.add("Exit");
- mainMenuBar.add(menu1);
-
- menu2 = new java.awt.Menu("Edit");
- menu2.add("Cut");
- menu2.add("Copy");
- menu2.add("Paste");
- mainMenuBar.add(menu2);
-
- menu3 = new java.awt.Menu("Help");
- menu3.add("About");
- mainMenuBar.add(menu3);
- setMenuBar(mainMenuBar);
- //}}
- }
-
- public Frame1(String title) {
- this();
- setTitle(title);
- }
-
- public synchronized void show() {
- move(50, 50);
- super.show();
-
- // INITIALISATION - runs when form is first shown
- // Create the two Vectors
- thingsInStock = new Vector();
- thingsInBasket = new Vector();
-
- // Add items to thingsInStock Vector
- thingsInStock.addElement("Tin of Schpamm");
- thingsInStock.addElement("Pot of Schnoodles");
- thingsInStock.addElement("Jug of Hare");
- thingsInStock.addElement("Kettle of Fish");
- thingsInStock.addElement("Bombay Duck");
- thingsInStock.addElement("Duck-billed platypus");
- thingsInStock.addElement("Can of worms");
- thingsInStock.addElement("Hair of the dog");
- thingsInStock.addElement("Dog's bone");
- thingsInStock.addElement("PC Plus");
- // Show the items in List box(es) on screen
- UpdateListBoxes();
- }
-
- public boolean handleEvent(Event event) {
- if (event.id == Event.WINDOW_DESTROY) {
- hide(); // hide the Frame
- dispose();
- System.exit(0);
- return true;
- }
- if (event.target == StockList && event.id == Event.ACTION_EVENT) {
- StockList_DblClick(event);
- }
- if (event.target == BasketList && event.id == Event.ACTION_EVENT) {
- BasketList_DblClick(event);
- }
- return super.handleEvent(event);
- }
-
- public boolean action(Event event, Object arg) {
- if (event.target instanceof MenuItem) {
- String label = (String) arg;
- if (label.equalsIgnoreCase("Open...")) {
- Open_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("About")) {
- About_Action(event);
- return true;
- } else
- if (label.equalsIgnoreCase("Exit")) {
- Exit_Action(event);
- return true;
- }
- }
- return super.action(event, arg);
- }
-
- static public void main(String args[]) {
- (new Frame1()).show();
- }
-
- //{{DECLARE_CONTROLS
- java.awt.FileDialog OpenFileDialog;
- java.awt.List StockList;
- java.awt.List BasketList;
- java.awt.Label label1;
- java.awt.Label label2;
- java.awt.Label label3;
- java.awt.Label label4;
- //}}
-
- //{{DECLARE_MENUS
- java.awt.MenuBar mainMenuBar;
- java.awt.Menu menu1;
- java.awt.Menu menu2;
- java.awt.Menu menu3;
- //}}
- }
-